<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>身份证号码验证</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .input-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }
        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }
        input:focus {
            outline: none;
            border-color: #4a90e2;
            box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #3a80d2;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .valid {
            background-color: #e8f5e9;
            color: #2e7d32;
            border: 1px solid #c8e6c9;
        }
        .invalid {
            background-color: #ffebee;
            color: #c62828;
            border: 1px solid #ffcdd2;
        }
        .info {
            margin-top: 10px;
            padding: 10px;
            background-color: #f5f5f5;
            border-radius: 4px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>身份证号码验证</h1>
        <div class="input-group">
            <label for="idCard">请输入身份证号码：</label>
            <input type="text" id="idCard" placeholder="18位身份证号码" maxlength="18">
        </div>
        <button onclick="validateIdCard()">验证</button>
        <div id="result" class="result"></div>
    </div>

    <script>
        function validateIdCard() {
            const idCard = document.getElementById('idCard').value.trim();
            const resultElement = document.getElementById('result');
            let result = '';
            let info = '';
            let isValid = false;

            // 1. 基本格式校验
            if (!idCard) {
                result = '请输入身份证号码';
            } else if (idCard.length !== 18) {
                result = '身份证号码长度必须为18位';
            } else if (!/^\d{17}[\dXx]$/.test(idCard)) {
                result = '身份证号码格式不正确';
            } else {
                // 2. 提取各部分信息
                 let areaCode = idCard.substring(0, 6);
                 let birthYear = idCard.substring(6, 10);
                 let birthMonth = idCard.substring(10, 12);
                 let birthDay = idCard.substring(12, 14);
                 let sequenceCode = idCard.substring(14, 17);
                 let checkCode = idCard.substring(17).toUpperCase();

                // 3. 地址码校验 (简单校验，实际应用中可对接详细地址码库)
                const areaCodeNum = parseInt(areaCode, 10);
                if (areaCodeNum < 100000 || areaCodeNum > 999999) {
                    result = '地址码无效';
                } else {
                    // 4. 出生日期校验
                    const year = parseInt(birthYear, 10);
                    const month = parseInt(birthMonth, 10);
                    const day = parseInt(birthDay, 10);
                    const today = new Date();
                    const currentYear = today.getFullYear();
                    
                    if (year < 1900 || year > currentYear) {
                        result = '出生年份无效';
                    } else if (month < 1 || month > 12) {
                        result = '出生月份无效';
                    } else {
                        // 计算当月天数
                        const daysInMonth = new Date(year, month, 0).getDate();
                        if (day < 1 || day > daysInMonth) {
                            result = '出生日期无效';
                        } else {
                            // 5. 校验码校验
                            const factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                            const checkCodeMap = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
                            
                            let sum = 0;
                            for (let i = 0; i < 17; i++) {
                                sum += parseInt(idCard[i], 10) * factors[i];
                            }
                            
                            const calculatedCheckCode = checkCodeMap[sum % 11];
                            
                            if (calculatedCheckCode === checkCode) {
                                isValid = true;
                                result = '身份证号码有效';
                                // 提取额外信息
                                const gender = parseInt(sequenceCode, 10) % 2 === 1 ? '男' : '女';
                                info = `性别：${gender}<br>出生日期：${birthYear}年${birthMonth}月${birthDay}日<br>地址码：${areaCode}`;
                            } else {
                                result = '校验码错误，身份证号码无效';
                            }
                        }
                    }
                }
            }

            // 显示结果
            resultElement.innerHTML = `<strong>${result}</strong>${info ? '<div class="info">' + info + '</div>' : ''}`;
            resultElement.className = `result ${isValid ? 'valid' : 'invalid'}`;
            resultElement.style.display = 'block';
        }

        // 为输入框添加按键事件，支持回车键验证
        document.getElementById('idCard').addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                validateIdCard();
            }
        });
    </script>
</body>
</html>